package string;

public class Test {
    public static void main(String[] args) {
        String s1 = "www.tedu.cn";
        String s2 = "http://www.tarena.com.cn";
        String name1 = getHostName(s1);
        String name2 = getHostName(s2);
        System.out.println(name1);//tedu
        System.out.println(name2);//tarena
    }

    public static String getHostName(String url) {
        /*
        最完美的解决算法
        int index1 = url.indexOf(".")+1;
        int index2 = url.indexOf(".",index1);

        String str = url.substring(index1, index2);

        return str;

         */

        //投机取巧法
        int start =url.indexOf(".");

        if (url.endsWith(".com.cn")) {
            int end = url.lastIndexOf(".");
            end -= 4;
            return url.substring(start+1, end);
        }
        if (url.endsWith(".cn")) {
            int end = url.lastIndexOf(".");
            return url.substring(start+1, end);
        }
        return "";
    }

}
